package us.mooresr.webtroopmanager.client.activity;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.ui.RegistrationView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class RegistrationActivity extends MyActivity implements RegistrationView.Presenter {

	public RegistrationActivity(ClientFactory clientFactory) {
		super(clientFactory);

	}
	
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		RegistrationView registrationView = clientFactory.getRegistrationView();
		registrationView.setPresenter(this);
		registrationView.clearForm();
		panel.setWidget(registrationView.asWidget());
		
	}

	@Override
	public void registerNewUser(String username, String password, String realName, String emailAddress) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		@SuppressWarnings("unused")
		AsyncCallback<Boolean> requestCallback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Unable to send your request to the administrator.");
				
			}

			@Override
			public void onSuccess(Boolean result) {
				Window.alert("Your request has been sent to the administrator for approval.");
				
			}
			
		};
		
		@SuppressWarnings("unused")
		AsyncCallback<Boolean> approveCallback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Unable to create your login due to an internal error.");
				
			}

			@Override
			public void onSuccess(Boolean result) {
				Window.alert("Your account has been created and is ready for use.");
				
			}
			
		};
		
		AsyncCallback<Boolean> validateCallback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Unable to create your login due to an internal error.");
				
			}

			@Override
			public void onSuccess(Boolean result) {
				Window.alert("Your account has been created.  Please check your email for instructions on activating it.");
				
			}
			
		};


        // troopManagerService.emailRequestToAdmin(username, password, realName, emailAddress, requestCallback);
        // troopManagerService.autoApproveRequest(username, password, realName, emailAddress, approveCallback);
		troopManagerService.emailValidationToUser(username, password, realName, emailAddress, Window.Location.getHref(), validateCallback);
	}



}
